import Icon from '@/components/Icon';
import { useAppDispatch } from '@/store';
import {
  collectArticleAction,
  getArticleDetailAction,
  likeArticleAction,
} from '@/store/action/article';
import { DetailData } from '@/store/reducer/article';
import { Popup, Toast } from 'antd-mobile';
import { useState } from 'react';
import CommentInput from '../CommentInput';
import Share from '../Share';
import styles from './index.module.scss';

type CommentFooterProps = {
  type?: 'normal' | 'reply';
  detail?: DetailData;
  onClick?: () => void;
};
export default function CommentFooter({ type = 'normal', detail, onClick }: CommentFooterProps) {
  const [visible, setVisible] = useState(false);
  const [shareVisible, setShareVisible] = useState(false);

  const dispatch = useAppDispatch();

  const handleLike = async () => {
    const hasLike = detail?.attitude === 1;
    await dispatch(likeArticleAction(detail!.art_id, hasLike));
    dispatch(getArticleDetailAction(detail!.art_id));
    Toast.show({ content: hasLike ? '取消点赞' : '点赞成功' });
  };

  return (
    <div className={styles.root}>
      <div className="comment-footer">
        <div className="input-btn" onClick={() => setVisible(true)}>
          <Icon type="iconbianji" />
          <span>去评论</span>
        </div>

        {type === 'normal' && (
          <>
            <div className="action-item" onClick={() => onClick && onClick()}>
              <Icon type="iconbtn_comment" />
              <p>评论</p>

              {!!detail?.comm_count && <span className="bage">{detail?.comm_count}</span>}
            </div>
            <div className="action-item">
              <Icon
                onClick={handleLike}
                type={detail?.attitude === 1 ? 'iconbtn_like_sel' : 'iconbtn_like2'}
              />
              <p>点赞</p>
            </div>
          </>
        )}
        <div className="action-item">
          <Icon
            onClick={() => dispatch(collectArticleAction())}
            type={detail?.is_collected ? 'iconbtn_collect_sel' : 'iconbtn_collect'}
          />
          <p>收藏</p>
        </div>
        <div className="action-item" onClick={() => setShareVisible(true)}>
          <Icon type="iconbtn_share" />
          <p>分享</p>
        </div>
      </div>

      <Popup visible={visible}>
        <CommentInput onBack={() => setVisible(false)} />
      </Popup>

      <Popup visible={shareVisible}>
        <Share onCancel={() => setShareVisible(false)} />
      </Popup>
    </div>
  );
}
